﻿using System.Web;
using System.Web.Mvc;
using AmazeUI.bll;
using Ema.Filters;
using Ema.Models.XtModel;
using Newtonsoft.Json;

namespace Ema.Controllers
{
    /// <summary>
    /// The user controller.
    /// </summary>
    public class UserController : BaseController
    {
        private XtUserBll userBll = new XtUserBll();
        private XtRoleBll roleBll = new XtRoleBll();

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            ViewBag.RoleId = roleBll.GetSelectListItems();
            return this.View();
        }

        /// <summary>
        /// 检查
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        [HttpPost]
        public ActionResult Sien()
        {
            if (this.GetAdmin != null)
            {
                return this.GetJsonDb(this.GetAdmin);
            }
            else
            {
                return this.GetJsonDb("error");
            }

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(SubXtUser model)
        {
            if (ModelState.IsValid)
            {
                model.UsingIp = this.GetIp;
                int addXtUser = userBll.AddXtUser(model);
                return this.GetJsonDb(addXtUser);
            }
            return this.GetJsonDb("验证错误:" + JsonConvert.SerializeObject(model));
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        public ActionResult List()
        {
            return this.View();
        }

        /// <summary>
        /// 用户列表数据
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        public ActionResult ListData(int rows, int page, string wdname)
        {
            var pages = userBll.GetPage(rows, page, wdname);
            return this.GetJsonDb(pages);
        }

        /// <summary>
        /// 用户列表数据
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        public ActionResult Pwd()
        {
            return this.View();
        }
        /// <summary>
        /// 用户列表数据
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        [HttpPost]
        public ActionResult Pwd(SubUserPwd model)
        {
            if (this.ModelState.IsValid)
            {
                var b = userBll.EditPwd(this.GetAdmin.Id, model.Pwd, model.Npwd);
                if (b)
                {
                    ViewBag.ms = "修改成功";
                }
                else
                {
                    ViewBag.ms = "修改失败";
                }

            }

            return this.View();
        }
        /// <summary>
        /// 上传文件。
        /// </summary>
        /// <param name="file"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Up(HttpPostedFileBase file, string name)
        {
            var b = this.BaseUp(file);

            return this.GetJsonDb(data: name);
        }

        /// <summary>
        /// 上传文件。
        /// </summary>
        /// <param name="file"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult TestUp()
        {
            return this.View();
        }
    }
}
